<template>
  <div>
    <el-button type="primary" @click="openDialog()">新增门店</el-button>

    <el-table :data="page.records" stripe style="width: 100%; margin-top: 16px">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="storeName" label="门店名称" />
      <el-table-column prop="storeCode" label="门店编码" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="phone" label="电话" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button size="small" type="danger" @click="del(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next, total"
      :total="page.total"
      :page-size="page.size"
      :current-page="page.current"
      @current-change="changePage"
    />

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialog" title="新增门店" width="420px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="门店名称">
          <el-input v-model="form.storeName" />
        </el-form-item>
        <el-form-item label="门店编码">
          <el-input v-model="form.storeCode" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialog = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import request from '@/api/request'
import { ElMessage, ElMessageBox } from 'element-plus'

interface Store {
  id: number
  storeName: string
  storeCode: string
  address: string
  phone: string
}

interface Page {
  records: Store[]
  total: number
  size: number
  current: number
}

const page = reactive<Page>({
  records: [],
  total: 0,
  size: 10,
  current: 1
})

const dialog = ref(false)
const form = reactive({
  storeName: '',
  storeCode: '',
  address: '',
  phone: ''
})

const load = (current = 1) => {
  request
    .get('/admin/stores/page', { params: { current, size: page.size } })
    .then(res => Object.assign(page, res.data))
}
const changePage = (cur: number) => load(cur)

const openDialog = () => {
  Object.assign(form, { storeName: '', storeCode: '', address: '', phone: '' })
  dialog.value = true
}

const submit = () => {
  request.post('/admin/store', form).then(res => {
    dialog.value = false
    load(page.current)

    // 弹出店长账号密码
    ElMessageBox.alert(
      `店长账号：${res.data.username}\n初始密码：${res.data.password}`,
      '请保存店长账号密码',
      { confirmButtonText: '已保存' }
    )
  })
}

const del = (id: number) =>
  ElMessageBox.confirm('确定删除？', '提示').then(() =>
    request.delete(`/admin/stores/${id}`).then(() => {
      ElMessage.success('已删除')
      load(page.current)
    })
  )

onMounted(() => load())
</script>